package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import lombok.Getter;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * ClassName: OrderController
 * Package: com.atguigu.cloud.controller
 * Description:
 *
 * @Author 加油-ssj
 * @Create 2025/3/14 14:53
 * @Version 1.0
 */
@RestController
public class OrderController {

//    public static final String PaymentSrv_URL = "http://localhost:8001";//先写死，硬编码
    public static final String PaymentSrv_URL = "http://cloud-payment-service";//服务注册中心上的微服务名称

    @Resource
    private RestTemplate restTemplate;

    @PostMapping("/consumer/pay/add")
    public ResultData addPay(@RequestBody PayDTO payDTO){
//        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add",payDTO,ResultData.class);
        return restTemplate.exchange(PaymentSrv_URL + "/pay/add" , HttpMethod.POST, new HttpEntity<>(payDTO), ResultData.class).getBody();


    }

//    @GetMapping("/consumer/pay/del/{id}")
//    public ResultData deletePay(@PathVariable("id") Integer id){
//
//        return restTemplate.postForObject(PaymentSrv_URL+"/pay/del/"+id,id,ResultData.class);
//      //  return restTemplate.postForObject(PaymentSrv_URL+"/pay/del"+id,ResultData.class,id);
//
//    }
//    @GetMapping ("/consumer/pay/update")
//    public ResultData updatePay(PayDTO payDTO){
//        return restTemplate.postForObject(PaymentSrv_URL+"/pay/update",payDTO,ResultData.class);
//    }
    @DeleteMapping("/consumer/pay/del/{id}")
    public ResultData delOrder(@PathVariable("id") Integer id) {
        return restTemplate.exchange(PaymentSrv_URL + "/pay/del/" + id, HttpMethod.DELETE, null, ResultData.class).getBody();
    }

    @PutMapping("/consumer/pay/update")
        public Object delOrder(@RequestBody PayDTO payDTO) {
            return restTemplate.exchange(PaymentSrv_URL + "/pay/update", HttpMethod.PUT, new HttpEntity<>(payDTO), ResultData.class).getBody();
        }




    @GetMapping("/consumer/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id){
//        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/"+id, ResultData.class,id);
        return restTemplate.exchange(PaymentSrv_URL+"/pay/get/"+id,HttpMethod.GET,null,ResultData.class).getBody();
    }

    @GetMapping("/consumer/pay/getAll")
    public ResultData getAll(){

        return restTemplate.getForObject(PaymentSrv_URL+"/pay/getAll",ResultData.class);
    }

//    @GetMapping("/consumer/pay/get/info")
//    public ResultData getInfo(){
//
//        return restTemplate.exchange(PaymentSrv_URL+"pay/get/info",HttpMethod.GET,null,ResultData.class).getBody();
//    }

    @GetMapping(value = "/consumer/pay/get/info")
    private String getInfoByConsul()
    {
        return restTemplate.exchange(PaymentSrv_URL + "/pay/get/info", HttpMethod.GET,null,String.class).getBody();
    }

//    @GetMapping(value = "/consumer/pay/get/info")
//    private ResultData getInfoByConsul()
//    {
//        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", ResultData.class);
//    }

//    @GetMapping(value = "/consumer/pay/get/info")
//    private String getInfoByConsul()
//    {
//        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
//    }
//
//    @GetMapping("/pay/getInfo")
//    public Object getInfo() {
//        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/Info", Result.class);
//    }


 }
